package agend.client.gui.panels.contacts;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import myContacts.Contact;
import myContacts.FamilyName;
import myContacts.FullName;
import myContacts.GivenName;
import myContacts.Name;
import myContacts.Nickname;

public class MyListCellRender implements ListCellRenderer<Contact> {

	private JPanel panel;
	private JLabel labelName;
	private JLabel labelNikname;

	public MyListCellRender() {
		super();
	}

	@Override
	public Component getListCellRendererComponent(
			JList<? extends Contact> list, Contact value, int index,
			boolean isSelected, boolean cellHasFocus) {

		initComponent();

		if(isSelected)
			panel.setBackground(Color.BLUE);
		else
			panel.setBackground(Color.WHITE);

		Name name = value.getName();

		if(name!=null)
		{
			FamilyName familyName = name.getFamilyName();
			FullName fullName = name.getFullName();
			GivenName givenName = name.getGivenName();

			if( fullName != null )
				labelName.setText(fullName.getValue());
			else if( givenName != null )
				labelName.setText(givenName.getValue());
			else if( familyName != null )
				labelName.setText(familyName.getValue());
		}
		
		Nickname nickname = value.getNickname();

		if( nickname != null && nickname.getValue()!=null)
			labelNikname.setText(nickname.getValue());

		return panel;
	}

	private void initComponent()
	{
		panel = new JPanel();

		BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
		panel.setLayout(boxLayout);

		labelName = new JLabel();
		labelName.setFont(new Font("TimesRoman", Font.PLAIN,  16));
		labelName.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(labelName);

		labelNikname = new JLabel();
		labelNikname.setFont(new Font("TimesRoman", Font.ITALIC,  12));
		labelNikname.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.add(labelNikname);
	}

}
